using UnityEngine;
using System.IO;
using UnityEngine.EventSystems;
using UnityEngine.UI;

#if UNITY_EDITOR
using UnityEditor;
#endif

public class CreateFloderTool
{
#if UNITY_EDITOR
    [MenuItem("Tools/CreateFolder #&_b")]
    private static void GenerateFolder()
    {

        string prjPath = Application.dataPath + "/";
        Directory.CreateDirectory(prjPath + "Audio");
        Directory.CreateDirectory(prjPath + "abRes/Assetbundle/prefabs/panelview");
        Directory.CreateDirectory(prjPath + "abRes/Assetbundle/prefabs/singleview");
        Directory.CreateDirectory(prjPath + "abRes/DataTable");
        Directory.CreateDirectory(prjPath + "Prefabs");
        Directory.CreateDirectory(prjPath + "Materials");
        Directory.CreateDirectory(prjPath + "Resources");
        Directory.CreateDirectory(prjPath + "Resources/DataTable");//Excel文件位置
        Directory.CreateDirectory(prjPath + "Scripts/0.Constant");
        Directory.CreateDirectory(prjPath + "Scripts/1.Manager");
        //Directory.CreateDirectory(prjPath + "Scripts/2.UI");
        //Directory.CreateDirectory(prjPath + "Scripts/2.UI/Panel");
        Directory.CreateDirectory(prjPath + "Scripts/2.UI/PanelView/PanelSetting");
        //Directory.CreateDirectory(prjPath + "Scripts/2.UI/SingleView");
        Directory.CreateDirectory(prjPath + "Scripts/2.UI/SingleView/SingleSetting");
        Directory.CreateDirectory(prjPath + "Scripts/3.Util");
        //Directory.CreateDirectory(prjPath + "Scripts/4.Class");
        Directory.CreateDirectory(prjPath + "Scripts/4.Class/ChildClass");
        Directory.CreateDirectory(prjPath + "Scripts/Editor");
        Directory.CreateDirectory(prjPath + "Scripts/Framework");
        Directory.CreateDirectory(prjPath + "Textures");
        Directory.CreateDirectory(prjPath + "Scenes");
        Directory.CreateDirectory(prjPath + "Animation");
        Directory.CreateDirectory(prjPath + "AnimationController");
        //Directory.CreateDirectory(prjPath + "Plugins");
        Directory.CreateDirectory(prjPath + "Plugins/iOS");
        Directory.CreateDirectory(prjPath + "Plugins/Android");
        Directory.CreateDirectory(prjPath + "Editor");
        Directory.CreateDirectory(prjPath + "Particle");
        Directory.CreateDirectory(prjPath + "Meshes");
        Directory.CreateDirectory(prjPath + "Shaders");
        Directory.CreateDirectory(Application.streamingAssetsPath);
        Directory.CreateDirectory(Application.streamingAssetsPath+ "/res/DataTable");
        
        AssetDatabase.Refresh();//刷新unity资源显示
        Debug.Log("文件夹创建完成");
        
        
    }
    [MenuItem("Tools/CreateInitialCanvas")]
    private static void CreateInitialCanvas()
    {
        // 检查场景中是否已存在名为"MyUIPanel"的对象
        GameObject existingPanel = GameObject.Find("BackPanel");
        if (existingPanel != null)
        {
            // 如果存在，选择该对象便于查看或修改，并提示用户
            Debug.LogWarning("当前页面已经存在初始页面");
            Selection.activeGameObject = existingPanel;
            return; // 退出方法，不创建新的面板
        }

        // 如果不存在，创建新的UI面板
        GameObject BackPanel = new GameObject("BackPanel");
        GameObject MainPanel = new GameObject("MainPanel");
        GameObject HitPanel = new GameObject("HitPanel");
        RectTransform rectTransform1 = BackPanel.AddComponent<RectTransform>();
        RectTransform rectTransform2 = MainPanel.AddComponent<RectTransform>();
        RectTransform rectTransform3 = HitPanel.AddComponent<RectTransform>();
        rectTransform1.sizeDelta = new Vector2(1080, 2400);
        rectTransform2.sizeDelta = new Vector2(1080, 2400);
        rectTransform3.sizeDelta = new Vector2(1080, 2400);

        // 添加到Canvas
        AddToCanvas(BackPanel);
        AddToCanvas(MainPanel);
        AddToCanvas(HitPanel);

        // 设置RectTransform使面板大小与屏幕大小一致
        rectTransform1.anchorMin = new Vector2(0, 0);
        rectTransform1.anchorMax = new Vector2(1, 1);
        rectTransform1.offsetMin = new Vector2(0, 0);
        rectTransform1.offsetMax = new Vector2(0, 0);   
        
        rectTransform2.anchorMin = new Vector2(0, 0);
        rectTransform2.anchorMax = new Vector2(1, 1);
        rectTransform2.offsetMin = new Vector2(0, 0);
        rectTransform2.offsetMax = new Vector2(0, 0);  
        
        rectTransform3.anchorMin = new Vector2(0, 0);
        rectTransform3.anchorMax = new Vector2(1, 1);
        rectTransform3.offsetMin = new Vector2(0, 0);
        rectTransform3.offsetMax = new Vector2(0, 0);

        EnsureEventSystem();
        // 确保新创建的面板被选中
        Selection.activeGameObject = MainPanel;
    }
    static void AddToCanvas(GameObject panel)
    {
        Canvas canvas = GameObject.FindObjectOfType<Canvas>();
        if (!canvas)
        {
            GameObject canvasObject = new GameObject("Canvas");
            canvas = canvasObject.AddComponent<Canvas>();
            canvasObject.AddComponent<CanvasScaler>();
            canvasObject.AddComponent<GraphicRaycaster>();
            
            canvas.renderMode = RenderMode.ScreenSpaceCamera;
            canvas.worldCamera = Camera.main;
            // 设置Canvas的UI Scale Mode为Scale With Screen Size
            CanvasScaler canvasScaler = canvasObject.GetComponent<CanvasScaler>();
            canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
            // 将Canvas设置为Main Camera的子对象
            canvasObject.transform.SetParent(Camera.main.transform, false);
            
        }
        panel.transform.SetParent(canvas.transform, false);
    }
    
    static void EnsureEventSystem()
    {
        EventSystem eventSystem = GameObject.FindObjectOfType<EventSystem>();
        if (eventSystem == null)
        {
            GameObject eventSystemObject = new GameObject("EventSystem");
            eventSystemObject.AddComponent<EventSystem>();
            eventSystemObject.AddComponent<StandaloneInputModule>(); // 添加StandaloneInputModule组件以支持鼠标和键盘输入
            // 可选：如果需要触摸输入，也可以添加TouchInputModule组件
        }
    }
   
#endif
    
}
